package com.crm.controller.settings;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.VariablesDAO;
import com.crm.entity.Business;
import com.crm.entity.Variables;

@Controller
@RequestMapping("/variables")
public class VariablesController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest request, HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		VariablesDAO vdao=new VariablesDAO();
		Variables v=vdao.getVariables(busid);
		if(v==null){
			v=new Variables();
		}
		return model=new ModelAndView("/jsp/Variables.jsp","command" ,v)
		.addObject("submenu", "reports")
		.addObject("menuName","reports");
	}
	@RequestMapping("/update.do")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model, BindingResult result,Variables variables) throws Exception{
		logger.info("In Controller Starting");
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		VariablesDAO vdao=new VariablesDAO();
		variables.setBusiness(bus);
		logger.info("Variables Values Are:"+variables);
		vdao.insert(variables);
		return model=new ModelAndView("/jsp/Variables.jsp","command",variables)
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("msg","Variables Are Successfully Updated");
	}
	
}
